package jmu.zzh.alumnus.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jmu.zzh.alumnus.business.entity.Donation;
import jmu.zzh.alumnus.business.mapper.DonationMapper;
import jmu.zzh.alumnus.business.request.DonationRequest;
import jmu.zzh.alumnus.business.service.DonationService;
import jmu.zzh.alumnus.common.util.SnowflakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DonationServiceImpl implements DonationService {

    @Autowired
    private DonationMapper donationMapper;

    @Override
    public int createDonation(Long userId, DonationRequest request) {
        Donation donation = new Donation();
        donation.setId(SnowflakeUtil.getId());
        donation.setUserId(userId);
        donation.setType(request.getType());
        donation.setAmount(request.getAmount());
        donation.setItemDesc(request.getItemDesc());
        donation.setDonateTime(new Date());
        return donationMapper.insert(donation);
    }

    @Override
    public PageInfo<Donation> getMyDonations(Long userId, int page, int size) {
        PageHelper.startPage(page, size);
        List<Donation> donations = donationMapper.selectByUserId(userId);
        return new PageInfo<>(donations);
    }

    @Override
    public PageInfo<Donation> getAllDonations(int page, int size) {
        PageHelper.startPage(page, size);
        List<Donation> donations = donationMapper.selectAll();
        return new PageInfo<>(donations);
    }
} 